﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CJJCH.Web.Manage
{
    public partial class NoteList : CJJCH.Web.Manage.PowerBase
    {
        CJJCH.BLL.T_NET_Note bll_note = new CJJCH.BLL.T_NET_Note();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitRequest();
                ReapterDataBinding();
            }

            LTP.Common.ButtonClass.ButtonOnClick(this.Page, btnSubmit, "checkform()", false);
        }
        protected void ReList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {

            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {

                ((LinkButton)e.Item.FindControl("lbntNote")).Text = ((CJJCH.Model.T_NET_Note)e.Item.DataItem).MenuThird.MenuName;
                ((LinkButton)e.Item.FindControl("lbntNote")).CommandArgument = ((CJJCH.Model.T_NET_Note)e.Item.DataItem).ID.ToString();
                if (((CJJCH.Model.T_NET_Note)e.Item.DataItem).MenuThird.MenuName == null)
                {
                    none.Style.Add("display", "none");

                }

                if (lblnoteID.Text != "")
                {
                    if (lblnoteID.Text == ((CJJCH.Model.T_NET_Note)e.Item.DataItem).ID.ToString())
                    {
                        txtContent.InnerText = ((CJJCH.Model.T_NET_Note)e.Item.DataItem).DataNote;

                    }
                }
                else
                {
                    if (e.Item.ItemIndex == 0)
                    {
                        if (((CJJCH.Model.T_NET_Note)e.Item.DataItem).MenuThird.MenuName != null)
                        {
                            lblDetails.Text = ((CJJCH.Model.T_NET_Note)e.Item.DataItem).MenuThird.MenuName + "详情";
                        }
                        lblnoteID.Text = ((CJJCH.Model.T_NET_Note)e.Item.DataItem).ID.ToString();
                        txtContent.InnerText = ((CJJCH.Model.T_NET_Note)e.Item.DataItem).DataNote;
                        lblThirdid.Text = ((CJJCH.Model.T_NET_Note)e.Item.DataItem).ThirdID.ToString();
                    }
                }
            }
        }
        private void InitRequest()
        {
            if (Request.QueryString["secondid"] != null && LTP.Common.CheckInput.CheckDigit(Request.QueryString["secondid"]))
            {

                CJJCH.Model.T_NET_MenuSecond model = new Model.T_NET_MenuSecond();
                model = (new CJJCH.BLL.T_NET_MenuSecond()).GetModel(Convert.ToInt32(Request.QueryString["secondid"]));
                if (model != null)
                {
                    lblSecondId.Text = LTP.Common.StringClass.EncodeString(model.ID.ToString());
                    lblSecondName.Text = LTP.Common.StringClass.EncodeString(model.MenuName);
                    lblFirstID.Text = LTP.Common.StringClass.EncodeString(model.FirstID.ToString());
                    lblDetails.Text = lblSecondName.Text + "详情";
                    NoteTop1.SecondId = Convert.ToInt32(lblSecondId.Text);
                    NoteTop1.SecondName = "地方标准"+lblSecondName.Text;
                    NoteTop1.SecondName1 = "企业标准备案申报程序";
                }
                else
                {
                    LTP.Common.WindowClass.WindowBack("操作错误！");
                }
            }
            if (Request.QueryString["Noteid"] != null && LTP.Common.CheckInput.CheckGUID(Request.QueryString["Noteid"]))
            {
                lblnoteID.Text = LTP.Common.StringClass.EncodeUrl(Request.QueryString["Noteid"].Trim());
            }



        }
        private void ReapterDataBinding()
        {
            string keyword = LTP.Common.StringClass.EncodeString(lblSecondId.Text.Trim());
            int fID = 0, sID = 0;

            if (LTP.Common.CheckInput.CheckDigit(lblFirstID.Text))
            {
                fID = Convert.ToInt32(lblFirstID.Text);
            }
            else
            {
                fID = 0;
            }

            if (LTP.Common.CheckInput.CheckDigit(lblSecondId.Text))
            {
                sID = Convert.ToInt32(lblSecondId.Text);
            }
            else
            {
                sID = 0;
            }

            IList<CJJCH.Model.T_NET_Note> list = new List<CJJCH.Model.T_NET_Note>();
            list = bll_note.GetList(sID);
            if (list.Count > 0)
            {
                this.ReList.DataSource = list;
                this.ReList.DataBind();
            }
            else
            {
                none.Style.Add("display", "none");
            }
        }
        protected void lbntNote_Click(object sender, EventArgs e)
        {

            if (LTP.Common.CheckInput.CheckGUID(((LinkButton)sender).CommandArgument))
            {
                CJJCH.Model.T_NET_Note model = new Model.T_NET_Note();
                model = bll_note.NotModel(new Guid(((LinkButton)sender).CommandArgument));
                txtContent.InnerText = model.DataNote;
                lblnoteID.Text = model.ID.ToString();
                lblThirdid.Text = model.ThirdID.ToString();
                lblDetails.Text = model.MenuThird.MenuName;
            }
            else
            {
                LTP.Common.WindowClass.WindowBack("操作错误");
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string error = "";
            CJJCH.Model.T_NET_Note note;
            if (this.txtContent.Value != "")
            {
                note = new Model.T_NET_Note();
                note.DataNote = LTP.Common.StringClass.RemoveSQL(this.txtContent.Value.Trim());
                Guid Nid = new Guid("00000000-0000-0000-0000-000000000000");
                if (lblnoteID.Text != "")
                {
                    Nid = new Guid(lblnoteID.Text);
                }
                else
                {
                    Nid = Guid.NewGuid();
                    lblnoteID.Text = Nid.ToString();
                }

                note.ID = Nid;
                note.ManagerID = SessionManagerID;
                note.SecondID = Convert.ToInt32(lblSecondId.Text);
                note.FirstID = Convert.ToInt32(lblFirstID.Text);
                int ThirdID = 0;
                if (lblThirdid.Text != "")
                {
                    ThirdID = Convert.ToInt32(lblThirdid.Text);
                }
                note.ThirdID = Convert.ToInt32(ThirdID);
                note.BuildDate = DateTime.Now;
                error = bll_note.NoteEdit(note);
                if (error == "")
                {
                    LTP.Common.WindowClass.WindowGoHref("更新成功！", "NoteList.aspx?secondid=" + LTP.Common.StringClass.EncodeUrl(lblSecondId.Text) + "&firstID=" + LTP.Common.StringClass.EncodeUrl(lblFirstID.Text) + "&secondname=" + LTP.Common.StringClass.EncodeUrl(lblSecondName.Text) + "&Noteid=" + lblnoteID.Text);
                }
                else
                {
                    LTP.Common.WindowClass.WindowBack(error);
                }
            }
            else
            {
                LTP.Common.WindowClass.WindowBack("请输入你要提交的内容！！");
            }
        }

    }
}